package cn.edu.bjtu.toychange.dao;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;

import org.hibernate.Query;
import org.hibernate.Session;

import cn.edu.bjtu.toychange.dao.support.SearchCondition;
import cn.edu.bjtu.toychange.entity.Toy;
import cn.edu.bjtu.toychange.entity.User;

public class ToyDao extends BaseDao {
	private Session session;
	
	public ToyDao() {
		session = getSession();
	}
	// 添加或更新玩具
	public boolean saveToy(Toy toy) {
		session.beginTransaction();
		
		try {
			session.saveOrUpdate(toy);
		} catch (Exception e) {
			return false;
		}
		
		session.close();
		
		return true;
	}

	// 删除玩具
	public boolean deleteToy(int toyID) {
		Session session = getSession();

		session.beginTransaction();
		
		try {
			session.delete(session.load(Toy.class, toyID));
		} catch (Exception e) {
			return false;
		}
		
		session.close();
		
		return true;
	}

	// 获取玩具
	public Toy getToy(int toyID) {
		return (Toy) getSession().get(Toy.class, toyID);
	}

	// 获取玩具主人
	public User getOwner(int toyID) {
		return getToy(toyID).getUser();
	}
	
	public boolean changeToyLock(int toyID, int state) {
		Toy toy = this.getToy(toyID);
		
		try {
			toy.setT_state(state);
			this.saveToy(toy);
		} catch (Exception e) {
			return false;
		}
		
		return true;
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList<Toy> getAllToys() {
		session.beginTransaction();
		ArrayList<Toy> toyList = (ArrayList<Toy>) session.createQuery("SELECT * FROM TOY").list();
		session.close();
		
		return toyList;
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList<Toy> getUserAllToys(int userID) {
		session.beginTransaction();
		ArrayList<Toy> toyList = (ArrayList<Toy>) session.createQuery("SELECT * FROM TOY WHERE T_userID = ?")
				.setInteger(0, userID).list();
		session.close();
		
		return toyList;
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList<Toy> getAvailToys(int userID) {
		session.beginTransaction();
		ArrayList<Toy> toyList = (ArrayList<Toy>) session.createQuery("SELECT * FROM TOY where T_userID = ? AND T_state = 2")
				.setInteger(0, userID).list();
		session.close();
		
		return toyList;
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList<Toy> getToysByType(int type) {
		session.beginTransaction();
		ArrayList<Toy> toyList = (ArrayList<Toy>) session.createQuery("SELECT * FROM where T_type = ?")
				.setInteger(0, type).list();
		session.close();
		
		return toyList;
	}
	
	public ArrayList<Toy> getRandomToy() {
		session.beginTransaction();
		ArrayList<Toy> toyList = new ArrayList<Toy>();
		ArrayList<Toy> allToy = this.getAllToys();
		int num = allToy.size();
		
		if (num > 5) {
			HashSet<Integer> hs = new HashSet<Integer>();
			Random rand = new Random();
			
			while (hs.size() < 5) {
				hs.add(rand.nextInt(num) + 1);
			}
			
			Iterator<Integer> iterator = hs.iterator();
			if (iterator.hasNext()) {
				toyList.add(allToy.get(iterator.next()));
			}
			
			return toyList;
		} else {
			return allToy;
		}
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList<Toy> searchToys(SearchCondition searchCondition) {
		session.beginTransaction();
		Query query = session.createQuery("Select * FROM toy WHERE T_intialPrice < ? AND T_intialPrice > ? AND "
				+ "T_newoldDegree < ? AND T_newoldDegree > ? AND T_lowAge > ? AND T_highAge < ?")
				.setDouble(0, searchCondition.getHighAge()).setDouble(1, searchCondition.getLowPrice())
				.setDouble(2, searchCondition.getHighNewoldDegree()).setDouble(3, searchCondition.getLowNewoldDegree())
				.setInteger(4, searchCondition.getLowAge()).setInteger(5, searchCondition.getHighAge());
		ArrayList<Toy> toyList = new ArrayList<Toy>();
		
		toyList = (ArrayList<Toy>) query.list();
		
		if (searchCondition.getKeyword() != null) {
			for (Toy t : toyList) {
				if ((t.getT_toyName().indexOf(searchCondition.getKeyword()) == -1) && (t.getT_description().indexOf(searchCondition.getKeyword()) == -1)) {
					toyList.remove(t);
				}
			}
		}
		
		if (searchCondition.getType() != null) {
			for (Toy t : toyList) {
				if (!t.getT_type().equals(searchCondition.getType())) {
					toyList.remove(t);
				}
			}
		}
		
		return toyList;
	}
}
